package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.SearchCatBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.CateSelectAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import de.greenrobot.event.EventBus;

public class CateSelectActivity extends BaseActivity {


    CateSelectAdapter cateSelectAdapter;
    UserPresenter presenter;
    List<SearchCatBean.DataBean> list;
    int type;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.cate_select_list)
    ListView cateSelectList;
    @BindColor(R.color.white)
    int white;
    @Bind(R.id.cate_select_all)
    RelativeLayout cateSelectAll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cate_select);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        list = new ArrayList<>();
        cateSelectAdapter = new CateSelectAdapter(this, list);
        presenter = new UserPresenterImp();
        type = getIntent().getIntExtra(Constant.TO_CATE_SCREEN, 0);

        initToolbar();
        initList();

        if (type == Constant.COMPANY) {
            presenter.getCompanySearchCat();
        } else {
            presenter.getProductSearchCat();
        }
        showLoading();
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(SearchCatBean bean) {
        hideLoading();
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            cateSelectAdapter.notifyDataSetChanged();
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }

    }

    private void initList() {
        cateSelectList.setAdapter(cateSelectAdapter);
        cateSelectList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(Constant.SEARCH_CATE_TITLE, list.get(position).getTitle());
                intent.putExtra(Constant.SEARCH_CATE_ID, list.get(position).getId());
                setResult(Constant.SEARCH_CATE_TO_RESULT, intent);
                finish();
            }
        });
    }

    private void initToolbar() {
        toolbar.setTitle("筛选类目");
        toolbar.setTitleTextColor(white);
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.cate_select_all)
    public void onClick() {
        Intent intent = new Intent();
        intent.putExtra(Constant.SEARCH_CATE_TITLE, "");
        setResult(Constant.SEARCH_CATE_TO_RESULT, intent);
        finish();
    }
}
